package com.mathpractise.service.impl;

import com.mathpractise.model.Question;
import com.mathpractise.service.QuestionOptionGenerationStrategy;
import com.mathpractise.util.RandomUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 简单题目选项生成策略实现
 * 根据题目的正确答案生成合理的干扰选项
 */
@Service
public class SimpleQuestionOptionGenerationStrategy implements QuestionOptionGenerationStrategy {
    
    @Override
    public List<Integer> generateOptions(Question question, int optionCount) {
        Set<Integer> options = new HashSet<>();
        options.add(question.getCorrectAnswer());
        
        // 根据答案的大小生成合理范围的干扰选项
        int correctAnswer = question.getCorrectAnswer();
        int range;
        
        if (Math.abs(correctAnswer) <= 10) {
            range = 10;
        } else if (Math.abs(correctAnswer) <= 20) {
            range = 20;
        } else {
            range = Math.abs(correctAnswer) / 2;
        }
        
        while (options.size() < optionCount) {
            int option = RandomUtil.nextInt(
                Math.max(0, correctAnswer - range), 
                correctAnswer + range + 1);
            options.add(option);
        }
        
        List<Integer> optionList = new ArrayList<>(options);
        // 打乱选项顺序
        java.util.Collections.shuffle(optionList);
        return optionList;
    }
}